package com.aauedu.config;/*
package com.aauedu.config;

import com.aauedu.bean.Car;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;



*/
/**
 * @Configuration(proxyBeanMethods = false)
 * 1.表示WebConfig是一个配置类
 * 2.proxyBeanMethods = false 使用Lite模式（多例模式）
 *//*

@Configuration(proxyBeanMethods = false)
public class WebConfig {

    //注入bean WebMvcConfigurer
    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurer() {
            */
/***
             * 解读：
             * 1.在addFormatters方法中，增加一个自定义的转换器
             * 2.增加自定义转换器   String->Car
             * 3.增加的自定义转化器会注册到Converters容器中
             * 4.converters底层结构是ConcurrentHashMap内置有124个转换器
             * 5.一会使用debug来看到这些转换器
             * @param registry
             *//*

            @Override
            public void addFormatters(FormatterRegistry registry) {
                registry.addConverter(new Converter<String, Car>() {
                    //s就是前端页面传入的字符串数据
                    @Override
                    public Car convert(String s) {
//                        TODO 这里加入自定义的业务逻辑代码
//                        if (!StringUtils.isEmpty(s)) {
                        if (!ObjectUtils.isEmpty(s)) {
                            String[] split = s.split(",");
                            Car car = new Car();
                            car.setName(split[0]);
                            car.setPrice(Double.parseDouble(split[1]));
                            return car;
                        }
                        return null;
                    }
                });
            }
        };
    }
}
*/




import com.aauedu.bean.Monster;
import org.springframework.context.annotation.Bean;
import com.aauedu.bean.Car;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
//lite 模式下，直接返回新实例对象
@Configuration(proxyBeanMethods = false)
public class WebConfig {
    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addFormatters(FormatterRegistry registry) {
                //注册一个 String->Car : 底层和我们手写 Spring/SpringMvc 机制一样
                //仍然使用反射+注解+IO+动态代理那一套来玩，将该转换器注册到转换器 converters 的容器中
                // (converters 底层是 ConcurrentHashMap), 没有多么神奇, 如果感觉不明白回去看看
                // 老韩自己实现 Spring 底层机制, 就了然了
                  registry.addConverter(new Converter<String, Car>() {
                    @Override
                    public Car convert(String source) {
                        if(!ObjectUtils.isEmpty(source)){
                            Car car = new Car();
                            String[] split = source.split(",");
//                            System.out.println(split[0]);
//                            System.out.println(split[1]);
                            car.setName(split[0]);
                            car.setPrice(Double.parseDouble(split[1]));
                            return car;
                        }
                        return null;
                    }
                });
            }

        };

        //写法二
        /*return new WebMvcConfigurer() {
            @Override
            public void addFormatters(FormatterRegistry registry) {
                Converter<String, Car> myConverter = new Converter<String, Car>() {
                    @Override
                    public Car convert(String source) {
                        if(!ObjectUtils.isEmpty(source)){
                            Car car = new Car();
                            String[] split = source.split(",");
//                            System.out.println(split[0]);
//                            System.out.println(split[1]);
                            car.setName(split[0]);
                            car.setPrice(Double.parseDouble(split[1]));
                            return car;
                        }
                        return null;
                    }
                };*/
                //还可以添加更多的转换器
                //第二个转换器
               /* Converter<String, Monster> myConverter2 = new Converter<String, Monster>() {
                    @Override
                    public Monster convert(String source) {
                        return null;
                    }
                };
*/
                //第三个转换器
           /*     Converter<String, Car> myConverter3 = new Converter<String, Car>() {
                    @Override
                    public Car convert(String source) {
                        System.out.println("source-" + source);
                        return null;
                    }
                };
*/
                //2.添加转化器到converters
//                registry.addConverter(myConverter);
//                registry.addConverter(myConverter2);
//                registry.addConverter(myConverter3);
//            }
//        };
    }
}

